package com.bike.bikeserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bike.bikeserver.common.core.domain.CommonResult;
import com.bike.bikeserver.common.enums.UserKey;
import com.bike.bikeserver.common.utils.StringUtils;
import com.bike.bikeserver.domain.SysRole;
import com.bike.bikeserver.domain.User;
import com.bike.bikeserver.domain.WXUser;
import com.bike.bikeserver.mapper.SysRoleMapper;
import com.bike.bikeserver.mapper.UserMapper;
import com.bike.bikeserver.mapper.WXUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bike.bikeserver.constant.Constants.*;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/7/21 12:52
 */
@Service
public class SysRoleService
{
    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    WXUserMapper wxUserMapper;

    /**
     * 根据用户标识和用户来源查找用户权限信息
     *
     * @param userKey
     * @param userSource
     * @return
     */
    public SysRole getRoleByUserKey(String userKey, String userSource)
    {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getUserKey, userKey);
        wrapper.eq(SysRole::getUserSource, userSource);
        return sysRoleMapper.selectOne(wrapper);
    }

    /**
     * 获取所有系统角色
     *
     * @return
     */
    public CommonResult getSysRoleList()
    {
        List<SysRole> sysRoles = sysRoleMapper.selectList(null);
        return CommonResult.success(sysRoles);
    }

    /**
     * 根据用户来源或用户权限获取角色权限信息
     *
     * @param sth
     * @param info
     * @return
     */
    public CommonResult getRoleBySth(String sth, String info)
    {
        if (sth == null || StringUtils.isBlank(sth))
        {
            return CommonResult.warn("请勿提交空值！");
        }
        // 转换一下查询标签到标签值
        if ("react后台管理系统".equals(info))
        {
            info = UserKey.BEHIND.getValue();
        }
        if ("WeChat前台小程序".equals(info))
        {
            info = UserKey.FRONT.getValue();
        }
        if ("可编辑".equals(info))
        {
            info = USER_RIGHT_EDIT;
        }
        if ("仅查看".equals(info))
        {
            info = USER_RIGHT_LOOK;
        }
        if ("最高权限".equals(info))
        {
            info = USER_RIGHT_ALL;
        }
        // 匹配标准
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if ("用户来源".equals(sth))
        {
            wrapper.like(SysRole::getUserSource, info);
        }
        else
        {
            wrapper.like(SysRole::getUserRight, info);
        }
        List<SysRole> sysRoles = sysRoleMapper.selectList(wrapper);
        return CommonResult.success("查询成功！为您找到" + sysRoles.size() + "条记录！", sysRoles);
    }

    /**
     * 根据id修改角色权限
     *
     * @param sysRole
     * @return
     */
    public CommonResult updateRoleRightById(SysRole sysRole)
    {
        if (sysRole.getUserRight() == null || StringUtils.isBlank(sysRole.getUserRight()))
        {
            return CommonResult.warn("请勿提交空值！");
        }
        SysRole sysRole1 = sysRoleMapper.selectById(sysRole.getId());
        if (sysRole.getUserRight().equals(sysRole1.getUserRight()))
        {
            return CommonResult.warn("请勿重复修改！");
        }
        if (USER_RIGHT_ALL.equals(sysRole1.getUserRight()))
        {
            return CommonResult.warn("不可降级root权限！");
        }
        if (USER_RIGHT_ALL.equals(sysRole.getUserRight()))
        {
            return CommonResult.warn("不可升级至root权限！");
        }
        sysRole1.setUserRight(sysRole.getUserRight());
        String userKey = sysRole1.getUserKey();
        // 同时修改用户类型
        if (UserKey.BEHIND.getValue().equals(sysRole.getUserSource()))
        {
            String userT = BACK_USER_LOOK;
            if (USER_RIGHT_EDIT.equals(sysRole.getUserRight()))
            {
                userT = BACK_USER_ADMIN;
            }
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getAccount, userKey);
            User user = userMapper.selectOne(wrapper);
            user.setUserType(userT);
            userMapper.updateById(user);
            sysRole1.setUserType(userT);
            sysRoleMapper.updateById(sysRole1);
        }
        else
        {
            String userT2 = FRONT_USER;
            if (USER_RIGHT_EDIT.equals(sysRole.getUserRight()))
            {
                userT2 = FRONT_ADMIN;
            }
            LambdaQueryWrapper<WXUser> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WXUser::getOpenid, userKey);
            WXUser wxUser = wxUserMapper.selectOne(wrapper);
            wxUser.setUserType(userT2);
            wxUserMapper.updateById(wxUser);
            sysRole1.setUserType(userT2);
            sysRoleMapper.updateById(sysRole1);
        }
        return CommonResult.success("修改成功！");
    }
}
